package org.batatasframework.web.element;

public abstract class AbstractElementCollection<Ele>extends AbstractElementContent<ElementCollection<Ele>> {
	
	//protected Ele currentElement;
	
	private int currentIndex = 0;
	
	public AbstractElementCollection(String name) {
		super(name);
	}

	protected abstract Ele createElement();
	
	/** 当前正在处理的元素，如果没有，则创建一个 */
	public Ele currentElement(){
		return indexOf(currentIndex);
	}
	
	public int size(){
		return content.size();
	}
	
	public Ele indexOf(int index){
		int len = index - content.size();
		while(len--> 0){
			content.add(createElement());
		}
		return content.get(index);
	}
	
	public Ele nextElement(){
		currentIndex++;
		return indexOf(currentIndex);
	}
	
//	public boolean hasNext(){
//		return content.size() > (currentIndex + 1);
//	}
	
	public Ele addElement(Ele ele){
		currentIndex = content.add(ele);
		return ele;
	}
	
	public Ele addElement(@SuppressWarnings("unchecked") Ele...ele){
		currentIndex = content.add(ele);
		return content.get(currentIndex);
	}
}
